<?php
namespace Api\Controller;

class AdController extends ApiController {


    public function  randomPenny($total,$num,$min=0.01){
        $data=array();
        for($i=1;$i<$num;$i++){
            //随机安全上限
            $safe_total=($total-($num-$i)*$min)/($num-$i);
            //金额
            $money=mt_rand($min*100,$safe_total*100)/100;
            //$money=mt_rand($min*100,$safe_total*100);
            $total=$total-$money;
            $data[$i]=$money;
        }
        $data[$num] =$total;
        return $data;
    }

    public function test(){
        $data = $this->randomPenny(100,5);
        var_dump($data);
    }
    /**
     * 获取APP启动页及活动
     */
    public function init(){

        $path = './Data/AppInitC';
        if ( !file_exists ( $path ) ) {
            $config = [
                'logo'    => '',
                'title'   => '',
                'content' => ''
            ];
        } else {
            $config = unserialize ( file_get_contents ( $path ) );
        }
        $logo_id = $config['logo'];
        unset($config['logo']);
        $config['logo'] = D('picture')->Where('id='.$logo_id)->getField('path');
        response(1,$config);
    }

    public function getList() {
        $position = I('post.position', 'index_banner');

        empty(getAdPositions($position)) && response(-1, null, '参数有误');

        if($position =='index_banner'){
            $map['end_time']=array('egt',NOW_TIME);
            $map['position']=array('eq','index_banner');
            $list=M('ad')->field(array('id','cover_id'))->where($map)->order('sort desc')->select();
            $result=array();
            foreach ($list as $k=>$v){
                $result[$k]['id']=$v['id'];
                $result[$k]['image']='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]  .get_cover($v['cover_id'], 'path') ;
            }
            response(1, $result);
        }else if ($position =='category_header'){
            $map['end_time']=array('egt',NOW_TIME);
            $map['position']=array('eq','category_header');
            $list=M('ad')->field(array('id','cover_id'))->where($map)->order('sort desc')->select();
            $result=array();
            foreach ($list as $k=>$v){
                $result[$k]['id']=$v['id'];
                $result[$k]['image']='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]  .get_cover($v['cover_id'], 'path') ;
            }
            response(1, $result);
        }
        else{
            $category_id = I('post.category_id', 0);
            $group_id = I('post.group_id', 0);
            $prefix = C('DB_PREFIX');
            $model = M();
            $l_table = $prefix.('ad');
            $r_table = $prefix.('product');
            $model = $model->table($l_table.' l')->join($r_table.' r ON l.product_id = r.id');
            $filters['l.position'] = $position;
            $filters['l.begin_time'] = array('elt', NOW_TIME);
            $filters['l.end_time'] = array('egt', NOW_TIME);
            // 分类id不为空
            if ($category_id) {
                $categories = M('product_category')->where(array('path'=>array('like','%,'.$category_id.",%")))->select();
                $ids = array();
                foreach ($categories as $category) {
                    $ids[] = $category['id'];
                }
                $filters['r.category_id'] = array('in', $ids);
            }
            // 如果类型id不为空
            if ($group_id) {
                $categories = M('product_category')->where(array('gid'=>$group_id))->select();
                $ids = array();
                foreach ($categories as $category) {
                    $ids[] = $category['id'];
                }
                $filters['r.category_id'] = array('in', $ids);
            }
            $list = $model->where($filters)->field('l.*')->order('l.sort ASC, l.id DESC')->select();
            foreach ($list as &$item) {
                $item['image'] = get_url($item['cover_id']);
                $product = M('product')->find($item['product_id']);
                if($product['type']=='score'){
                	$item['product'] = array(
	                    'title' => $product['title'],
	                    'price' => $product['use_score'],
	                    'view' => $product['view']
                	);
                }else{
					$item['product'] = array(
	                    'title' => $product['title'],
	                    'price' => $product['price'],
	                    'view' => $product['view']
                	);
                }
                
            }
            response(1, $list);
        }
    }

    public function getdetail(){
        $id=I('post.id');
        $map['id']=array('eq',$id);
        $result=M('ad')->field(array('id','title','content','create_time','cover_id'))->where($map)->find();
        if(empty($result)){
            response(-1,'','没有内容');
        }
        $arr=array();
        $arr['id']=$result['id'];
        $arr['title']=$result['title'];
        $arr['content']=$result['content'];
        $arr['image']='http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"]  .get_cover($result['cover_id'], 'path') ;
        $arr['create_time']=$result['create_time'];
        response(1,array($arr));
    }
}
?>